/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.swing.JOptionPane;
import radiostation.AppControl;
import radiostation_POJO.Album;

/**
 *
 * @author eliastsourapas
 */
public class R8_GroupAlbum_View extends javax.swing.JFrame {
    public boolean createMode; //δημιουργία άλμπουμ
    public boolean editMode; //επεξεργασία άλμπουμ
    public boolean deleteMode; //διαγραφή άλμπουμ
    private List<Album> groupalbums = new ArrayList();
    private EntityManager em;
    public Album newAlbum; //Το άλμπουμ που τροποποιείται
    AppControl ap; //Αντικείμενο για τον χειρισμό της ΒΔ
    public static int selRow;

    /**
     * Creates new form R8_GroupAlbum_View
     */
    public R8_GroupAlbum_View() {
        initComponents();
    }
    public boolean getCreateMode() {
        return createMode;
    }
    
    public void setCreateMode(boolean createMode) {
        this.createMode = createMode;
    }
    
    public boolean getEditMode() {
        return editMode;
    }
    
    public void setEditMode(boolean editMode) {
        this.editMode = editMode;
    }
    
    public boolean getDeleteMode() {
        return deleteMode;
    }
    
    public void setDeleteMode(boolean deleteMode) {
        this.deleteMode = deleteMode;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        RadioStationPUEntityManager0 = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("RadioStationPU").createEntityManager();
        albumQuery = java.beans.Beans.isDesignTime() ? null : RadioStationPUEntityManager0.createQuery("SELECT a FROM Album a WHERE a.musicgroupId.musicgroupName IS NOT NULL");
        albumList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : albumQuery.getResultList();
        jLabel1 = new javax.swing.JLabel();
        addGroupAlbum = new javax.swing.JButton();
        deleteSelectedGroupAlbum = new javax.swing.JButton();
        editSelectedGroupAlbum = new javax.swing.JButton();
        ExitGroupAlbum = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableGroupAlbums = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Lucida Grande", 1, 18)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Άλμπουμ Συγκροτημάτων");

        addGroupAlbum.setText("Προσθήκη");
        addGroupAlbum.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addGroupAlbumActionPerformed(evt);
            }
        });

        deleteSelectedGroupAlbum.setText("Διαγραφή");
        deleteSelectedGroupAlbum.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteSelectedGroupAlbumActionPerformed(evt);
            }
        });

        editSelectedGroupAlbum.setText("Επεξεργασία");
        editSelectedGroupAlbum.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editSelectedGroupAlbumActionPerformed(evt);
            }
        });

        ExitGroupAlbum.setText("Έξοδος");
        ExitGroupAlbum.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ExitGroupAlbumActionPerformed(evt);
            }
        });

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, albumList, tableGroupAlbums);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${title}"));
        columnBinding.setColumnName("Τίτλος");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${musicgroupId.musicgroupName}"));
        columnBinding.setColumnName("Συγκρότημα");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${albumType}"));
        columnBinding.setColumnName("Τύπος");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${discNumber}"));
        columnBinding.setColumnName("Αριθμός Δίσκου");
        columnBinding.setColumnClass(Integer.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${releaseDate}"));
        columnBinding.setColumnName("Ημ/νία Κυκλοφορίας");
        columnBinding.setColumnClass(java.util.Date.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${mpcId.mpcName}"));
        columnBinding.setColumnName("Δισκογραφική");
        columnBinding.setColumnClass(String.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jScrollPane1.setViewportView(tableGroupAlbums);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(addGroupAlbum)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(deleteSelectedGroupAlbum)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(editSelectedGroupAlbum)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 322, Short.MAX_VALUE)
                        .addComponent(ExitGroupAlbum)))
                .addContainerGap(28, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(8, 8, 8)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 378, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addGroupAlbum)
                    .addComponent(deleteSelectedGroupAlbum)
                    .addComponent(editSelectedGroupAlbum)
                    .addComponent(ExitGroupAlbum))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void addGroupAlbumActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addGroupAlbumActionPerformed
        // TODO add your handling code here:
        this.createMode = true;
        this.editMode = false;
        this.deleteMode = false;
        this.setVisible(true);
        new R9_GroupAlbum_Management(this).setVisible(true);
    }//GEN-LAST:event_addGroupAlbumActionPerformed

    private void ExitGroupAlbumActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ExitGroupAlbumActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_ExitGroupAlbumActionPerformed

    private void deleteSelectedGroupAlbumActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteSelectedGroupAlbumActionPerformed
        // TODO add your handling code here:
        this.createMode = false;
        this.editMode = false;
        this.deleteMode = true;
        if(tableGroupAlbums.getSelectedRow()==-1){
            JOptionPane.showMessageDialog(null, "Δεν έχει επιλεγεί άλμπουμ!");
        }else{
            for(Album alb: groupalbums){
                if(alb.getAlbumId()==tableGroupAlbums.getValueAt(tableGroupAlbums.getSelectedRow(), 0)){
                    newAlbum = alb;
                    new R9_GroupAlbum_Management(this).setVisible(true);
                }
            }
        }
    }//GEN-LAST:event_deleteSelectedGroupAlbumActionPerformed

    private void editSelectedGroupAlbumActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editSelectedGroupAlbumActionPerformed
        // TODO add your handling code here:
        this.createMode = false;
        this.editMode = true;
        this.deleteMode = false;
        if(tableGroupAlbums.getSelectedRow()==-1){
            JOptionPane.showMessageDialog(null, "Δεν έχει επιλεγεί άλμπουμ!");
        }else{
            selRow = tableGroupAlbums.getSelectedRow();
            newAlbum = albumList.get(tableGroupAlbums.convertRowIndexToModel(selRow));
            new R9_GroupAlbum_Management(newAlbum).setVisible(true);
            /*for(Album alb: groupalbums){
                if(alb.getAlbumId()==tableGroupAlbums.getValueAt(tableGroupAlbums.getSelectedRow(), 0)){
                    newAlbum = alb;
                    new R13_ArtistAlbum_Management(newAlbum).setVisible(true);
                }
            }*/
        }
    }//GEN-LAST:event_editSelectedGroupAlbumActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(R8_GroupAlbum_View.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(R8_GroupAlbum_View.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(R8_GroupAlbum_View.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(R8_GroupAlbum_View.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new R8_GroupAlbum_View().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ExitGroupAlbum;
    private javax.persistence.EntityManager RadioStationPUEntityManager0;
    private javax.swing.JButton addGroupAlbum;
    private java.util.List<radiostation_POJO.Album> albumList;
    private javax.persistence.Query albumQuery;
    private javax.swing.JButton deleteSelectedGroupAlbum;
    private javax.swing.JButton editSelectedGroupAlbum;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tableGroupAlbums;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
